using System;

namespace Fornax020.SerialComm.Configuration
{
    /// <summary>
    /// 串口通信配置类
    /// 统一管理串口通信相关的配置参数
    /// </summary>
    public class SerialConfig
    {
        #region 串口基本配置
        /// <summary>
        /// 默认串口名称
        /// </summary>
        public string DefaultPort { get; set; } = "COM3";
        
        /// <summary>
        /// 默认波特率
        /// </summary>
        public int DefaultBaudRate { get; set; } = 9600;
        
        /// <summary>
        /// 数据位
        /// </summary>
        public int DataBits { get; set; } = 8;
        
        /// <summary>
        /// 停止位
        /// </summary>
        public System.IO.Ports.StopBits StopBits { get; set; } = System.IO.Ports.StopBits.One;
        
        /// <summary>
        /// 校验位
        /// </summary>
        public System.IO.Ports.Parity Parity { get; set; } = System.IO.Ports.Parity.None;
        
        /// <summary>
        /// 握手协议
        /// </summary>
        public System.IO.Ports.Handshake Handshake { get; set; } = System.IO.Ports.Handshake.None;
        #endregion

        #region 超时配置
        /// <summary>
        /// 读取超时时间（毫秒）
        /// </summary>
        public int ReadTimeout { get; set; } = 1000;
        
        /// <summary>
        /// 写入超时时间（毫秒）
        /// </summary>
        public int WriteTimeout { get; set; } = 1000;
        #endregion

        #region 重试配置
        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; } = 3;
        
        /// <summary>
        /// 重试延迟时间（毫秒）
        /// </summary>
        public int RetryDelayMs { get; set; } = 1000;
        #endregion

        #region 协议配置
        /// <summary>
        /// 起始字符
        /// </summary>
        public char StartOfText { get; set; } = (char)0x02;
        
        /// <summary>
        /// 结束字符
        /// </summary>
        public char EndOfText { get; set; } = (char)0x0D;
        #endregion

        #region 缓冲区配置
        /// <summary>
        /// 接收缓冲区大小
        /// </summary>
        public int BufferSize { get; set; } = 1024;
        #endregion

        #region 静态工厂方法
        /// <summary>
        /// 获取默认配置
        /// </summary>
        public static SerialConfig Default => new SerialConfig();

        /// <summary>
        /// 获取快速配置（高波特率）
        /// </summary>
        public static SerialConfig Fast => new SerialConfig
        {
            DefaultBaudRate = 115200,
            ReadTimeout = 500,
            WriteTimeout = 500,
            RetryCount = 2,
            RetryDelayMs = 500
        };

        /// <summary>
        /// 获取稳定配置（低波特率，长超时）
        /// </summary>
        public static SerialConfig Stable => new SerialConfig
        {
            DefaultBaudRate = 9600,
            ReadTimeout = 2000,
            WriteTimeout = 2000,
            RetryCount = 5,
            RetryDelayMs = 2000
        };
        #endregion
    }
}
